package cn.bingoo.handy;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import cn.bingoo.swing.utils.Swings;

public class SshFrame extends JFrame {
    private static final long serialVersionUID = 5480035063564006159L;
    private JPanel contentPane;

    private JTextPane textResult;
    private SshTuple sshTuple;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    SshFrame frame = new SshFrame();
                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SshFrame() {
        setTitle("SSH Operations");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 651, 473);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 112, 117, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPane.add(scrollPane, gbc_scrollPane);

        JPanel panel = new JPanel();
        scrollPane.setViewportView(panel);
        panel.setLayout(new BorderLayout(0, 0));

        final JTextPane textConfig = new JTextPane();
        textConfig
                .setText("#配置信息，每行格式为：IP:PORT, USER, PASS, DIR, LOG FILE\r\n10.142.195.61, bingoo, bingoo\r\n#10.142.164.88, mall, mall");
        panel.add(textConfig, BorderLayout.CENTER);

        JPanel panel_2 = new JPanel();
        GridBagConstraints gbc_panel_2 = new GridBagConstraints();
        gbc_panel_2.insets = new Insets(0, 0, 5, 0);
        gbc_panel_2.fill = GridBagConstraints.BOTH;
        gbc_panel_2.gridx = 0;
        gbc_panel_2.gridy = 1;
        contentPane.add(panel_2, gbc_panel_2);
        GridBagLayout gbl_panel_2 = new GridBagLayout();
        gbl_panel_2.columnWidths = new int[] { 0, 0, 0 };
        gbl_panel_2.rowHeights = new int[] { 0, 0 };
        gbl_panel_2.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
        gbl_panel_2.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
        panel_2.setLayout(gbl_panel_2);

        JPanel panel_3 = new JPanel();
        GridBagConstraints gbc_panel_3 = new GridBagConstraints();
        gbc_panel_3.insets = new Insets(0, 0, 0, 5);
        gbc_panel_3.fill = GridBagConstraints.BOTH;
        gbc_panel_3.gridx = 0;
        gbc_panel_3.gridy = 0;
        panel_2.add(panel_3, gbc_panel_3);
        GridBagLayout gbl_panel_3 = new GridBagLayout();
        gbl_panel_3.columnWidths = new int[] { 0, 0 };
        gbl_panel_3.rowHeights = new int[] { 0, 0, 0, 0 };
        gbl_panel_3.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
        gbl_panel_3.rowWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
        panel_3.setLayout(gbl_panel_3);

        final JButton btnExecCmds = new JButton("Exec Cmds");

        GridBagConstraints gbc_btnExecCmds = new GridBagConstraints();
        gbc_btnExecCmds.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnExecCmds.insets = new Insets(0, 0, 5, 0);
        gbc_btnExecCmds.gridx = 0;
        gbc_btnExecCmds.gridy = 0;
        panel_3.add(btnExecCmds, gbc_btnExecCmds);

        JButton btnClear = new JButton("Clear");

        GridBagConstraints gbc_btnClear = new GridBagConstraints();
        gbc_btnClear.anchor = GridBagConstraints.SOUTH;
        gbc_btnClear.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnClear.gridx = 0;
        gbc_btnClear.gridy = 2;
        panel_3.add(btnClear, gbc_btnClear);

        JScrollPane scrollPane_1 = new JScrollPane();
        GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
        gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
        gbc_scrollPane_1.gridx = 1;
        gbc_scrollPane_1.gridy = 0;
        panel_2.add(scrollPane_1, gbc_scrollPane_1);

        JPanel panel_4 = new JPanel();
        scrollPane_1.setViewportView(panel_4);
        panel_4.setLayout(new BorderLayout(0, 0));

        final JTextPane textCmds = new JTextPane();
        textCmds.setText("bingoo param AutoExpectTerminator=false\r\nbingoo expect $\r\ntelnet 10.142.195.64 11211\r\nbingoo expect Escape\r\nquit\r\nbingoo expect $");
        panel_4.add(textCmds, BorderLayout.CENTER);

        JScrollPane scrollPane_2 = new JScrollPane();
        GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
        gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
        gbc_scrollPane_2.gridx = 0;
        gbc_scrollPane_2.gridy = 2;
        contentPane.add(scrollPane_2, gbc_scrollPane_2);

        JPanel panel_1 = new JPanel();
        scrollPane_2.setViewportView(panel_1);
        panel_1.setLayout(new BorderLayout(0, 0));

        textResult = new JTextPane();
        sshTuple = new SshTuple(textResult);
        panel_1.add(textResult, BorderLayout.CENTER);

        btnClear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Swings.clearText(textResult);
            }
        });
        btnExecCmds.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // EventQueue.invokeLater(null);
                btnExecCmds.setEnabled(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sshTuple.execCmds(textConfig.getText(), textCmds.getText());
                        btnExecCmds.setEnabled(true);
                    }
                }).start();
            }
        });
    }

}
